const HOST = 'https://biapi.vaubang.cn/api/v2';
const app = getApp();

// var isRefreshing = true;

function req(obj) {
	var token = uni.getStorageSync('loginInfo').token;
	return new Promise((resolve, reject) => {
		var method = obj.method || "GET";
		var url = HOST + obj.url || "";
		var data = obj.data || {};
		var header = obj.header || {
			'Content-Type': obj.contentType || 'application/json',
			'Authorization': ('Bearer ' + token) || ''
		};
		var success = obj.success; // 成功回调函数
		var fail = obj.fail; //表示失败后，要执行的回调函数
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: ((res) => {
				// 当授权token过期时
				if (res.statusCode === 401) {
					uni.hideLoading();
					uni.removeStorageSync("isLogin");
					uni.removeStorageSync("loginInfo");
					if (token) {
						resolve({
							code: 401,
							data: [],
							msg: "登录授权过期"
						})
					} else {
						resolve({
							code: 400,
							data: [],
							msg: "未绑定帐号"
						})
					}
				} else if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					resolve({
						code: res.statusCode,
						data: [],
						msg: "失败"
					})
				}
			}),
			fail: ((err) => {
				reject(err)
			})
		})
	})
}
export default req
